#Tai-Hsien Ou Yang
#July 10
library('mecdf')


#/nfs/apps/R/2.9.0/bin/Rscript

setwd("/ifs/scratch/c2b2/ip_lab/to2232/lk")



FILE_INPUT_PED1="halfsiblings.lst" 
FILE_INPUT_LEN ="Lengths.txt"
FILE_INPUT_NAME ="PopIndex.txt"
FILE_OUTPUT_LEN ="x.hf.txt"
FILE_OUTPUT_PROB="p.hf.txt"
pair1<-scan(FILE_INPUT_PED1 , list(id1="",id2="")) #Parent-Child, directed network: search for the overlap of 2nd column of both lists.
idx<-scan(FILE_INPUT_NAME , list(id=""))
#data<-read.table(FILE_INPUT_LEN)



pair.list=matrix(0,length(pair1$id1),1) #col: common.id, id1, id2, pair.prob1, pair.prob2 



#dim(trio.list) <- c(nx, ny)
for ( i in 1:length(pair1$id1))  
{
	id1=pmatch(pair1$id1[i],idx$id)[1]
 	id2=pmatch(pair1$id2[i],idx$id)[1]
	pair.list[i,1]=as.numeric(data[id1,id2])
}



write.table(pair.list, file =FILE_OUTPUT_LEN,col.names=F,row.names=F,sep=",")
pcdf<-mecdf (pair.list, continuous=FALSE, validate=TRUE,  project=FALSE, expandf=0.1)


pair.eval=matrix(0,length(pair1$id1),1)
#Use the model to predict the likelihood
for(i in 1:length(trio.list[,1])) 
{
		pair.eval[i]<-pcdf(pair.list[i])	
}

write.table(pair.eval, file = FILE_OUTPUT_PROB,col.names=F,row.names=F,sep=",")
